﻿<extend name="Public:base" />
<block name="title"><title>重置密码</title></block>
<block name="header_styles">
    <link href="__Public__/js/kendo.ui/kendo.bootstrap.min.css" rel=" stylesheet" />
    <link href="__Public__/js/kendo.ui/kendo.common-bootstrap.min.css" rel=" stylesheet" />

    <style type="text/css">
        .main-wrapper .container {
            background-color: #ffffff;
        }
    </style>
</block>
<block name="main">
    <div class="reset-pwd-form" id="progress_container">
        <div style="border-bottom: 1px solid #dedede; font-size: 2rem;">
            <span>您可以通过如下方式重置密码</span>
        </div>
        <div class="reset-way">
            <div class="reset-way-title">方式一</div>
            <div class="reset-way-content">
                <eq name="pemailFlag" value="1">向密保邮箱 {$pemail} <a href="javascript:void(0);" data-email="{$user.email}" class="mailReset btn btn-white">发送邮件</a> 重置密码<else />您未设置密保邮箱，无法通过向邮箱发送邮件重置密码。</eq>
            </div>
            <div class="reset-way-title">方式二</div>
            <div class="reset-way-content">
                <notempty name="qlist">
                    <form id="form_reset">
                        回答如下密保问题<br />
                        <input type="hidden" id="questuid" value="{$qlist[0].uid}" />
                        <div class="reset-pwd-question">
                            问题1：{$qlist[0].name}？<span id="answer1-description" class="validation-error2"></span>
                            <input type="text" name="Answer1" id="Answer1" required maxlength="200" />
                            <input type="hidden" id="Quest1" value="{$qlist[0].qid}" />
                        </div>
                        <div class="reset-pwd-question">
                            问题2：{$qlist[1].name}？<span id="answer2-description" class="validation-error2"></span>
                            <input type="text" name="Answer2" id="Answer2" required maxlength="200" />
                            <input type="hidden" id="Quest2" value="{$qlist[1].qid}" />
                        </div>
                        <div class="text-center">
                            <input type='button' id='submitQuest' class="btn btn-white" value='验证' />
                        </div>
                    </form>
                    <else />
                    您未设置密保问题，无法通过回答密保问题重置密码。
                </notempty>
            </div>
            <br /><br />
            <p style="margin-left: 20px;">若以上两种方式都无法使用，请&nbsp;&nbsp;<a class='btn btn-default' href="{:U('/contactus')}">联系我们</a>&nbsp;&nbsp;重置密码</p>
        </div>
    </div>
</block>
<block name="footer_scripts">
    <div style="display: none;">
        <div class="box" id="boxMessages" style="width:450px;">
            <div class="box-title" id="boxTitle">密保问题验证</div>
            <div class="box-content text-center" id="boxMsgContent" style="padding: 30px 0px;"></div>
            <div class="box-footer text-center">
                <a href="javascript:void(0);" onclick="$.fancybox.close();" class="btn btn-white">关闭</a>
            </div>
        </div>
        <div class="box" id="boxResetPwd" style="width:450px;">
            <form id="formValidate">
                <div class="box-title">
                    <a href="javascript:void(0)" class="pull-right" style="color: #ffffff;" onclick="$.fancybox.close();"><i class="fa fa-times"></i></a>
                    密保问题验证
                </div>
                <div class="box-content text-center" style="padding: 30px 0px 10px 0px;">
                    验证通过，请重置密码！
                    <br /><br />
                    <div class="form-horizontal" style="margin: auto;width:340px;">
                        <div class="form-group">
                            <label class="col-sm-4 control-label">新密码</label>
                            <div class="col-sm-8">
                                <input id='newpwd' type="password" name="newpassword" class="form-control" data-val="true" data-val-required="请输入新密码" maxlength="50" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">确认新密码</label>
                            <div class="col-sm-8">
                                <input id="newpwdconfirm" type="password" name="newpassword" class="form-control" data-val="true" data-val-required="请确认新密码" maxlength="50" />
                            </div>
                        </div>
                    </div>
                    <p id='resetpwdnotice' style="display: none;color: #F00;margin-left: 20px;"></p>
                </div>
                <div class="box-footer text-center">
                    <input type='button' id='submitReset' class="btn btn-white" value='重置' />
                </div>
            </form>
        </div>
    </div>
    <script type="text/javascript">
        require(["jquery", "jquery.validate", "kendoui/kendo.web.min", "modules/message"], function ($, k, messagebox) {
            $('#form_reset').validate({
                errorPlacement: function (error, element) {
                    error.appendTo(element.parent(".reset-pwd-question").children("span:first"));
                },
                messages: {
                    Answer1: {
                        required: '此项必须填写'
                    },
                    Answer2: {
                        required: '此项必须填写',
                    }
                }
            });

            function showMessages(title, msg) {
                $("#boxTitle").text(title);
                $("#boxMsgContent").text(msg);
                $.fancybox.open("#boxMessages", { closeBtn: false, padding: 0 });
            }

            $(function () {

                $('#submitQuest').click(function () {
                    if ($('#form_reset').valid()) {
                        var url = "{:U('Login/verifyQuest_ajax')}";
                        var uid = $('#questuid').val();
                        var q1 = $('#Quest1').val();
                        var q2 = $('#Quest2').val();
                        var a1 = $('#Answer1').val();
                        var a2 = $('#Answer2').val();
                        $.post(url, { 'uid': uid, 'q1': q1, 'a1': a1, 'q2': q2, 'a2': a2 }, function (data) {
                            var vdata = eval(data);
                            if (vdata.success == 1) {
                                //显示密码重置框
                                $.fancybox.open("#boxResetPwd", { closeBtn: false, padding: 0 });
                            }
                            else {
                                showMessages('提示', vdata.msg);
                            }
                        }, 'json');
                    }
                });

                $('#submitReset').click(function () {
                    var url = "{:U('Login/resetpwd_ajax')}";
                    var newpwd = $('#newpwd').val();
                    var newpwdconfirm = $('#newpwdconfirm').val();
                    if (newpwd == '') {
                        $('#resetpwdnotice').show();
                        $('#resetpwdnotice').text('新密码输入框不能为空');
                        return false;
                    }
                    if (newpwdconfirm == '') {
                        $('#resetpwdnotice').show();
                        $('#resetpwdnotice').text('密码确认框不能为空');
                        return false;
                    }
                    if (newpwd != newpwdconfirm) {
                        $('#resetpwdnotice').show();
                        $('#resetpwdnotice').text('两次密码输入不一致');
                        return false;
                    }
                    $.post(url, { 'newpwd': newpwd }, function (data) {
                        var vdata = eval(data);
                        if (vdata.success == 1) {
                            alert('重置密码成功,请重新登陆!');
                            $.fancybox.close();
                            window.location.href = "{:U('Login/index')}";
                        }
                        else {
                            alert(vdata.msg);
                            return false;
                        }
                    }, 'json');
                });

                //邮件重置密码
                $(".mailReset").click(function () {
                    var email = $(this).data("email");
                    var url = "{:U('Login/mailpwd')}";
                    $.post(url, { 'email': email }, function (data) {
                        kendo.ui.progress($("#progress_container"), false);
                        var vdata = eval(data);
                        if (vdata.success == 1) {
                            messagebox.alert({ content: vdata.msg });
                        }
                        else {
                            messagebox.alert({ content: vdata.msg });
                        }
                    }, 'json');
                    kendo.ui.progress($("#progress_container"), true);
                });
            });
        });
    </script>
</block>
